Skip to content

Fix database bugs #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 7, 2025
Merged

Fix database bugs #13

merged 3 commits into from
Jul 7, 2025

Conversation

fulleni
Copy link
Member

@fulleni fulleni commented Jul 7, 2025

Status

READY/IN DEVELOPMENT/HOLD

Description

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

fulleni added 3 commits July 7, 2025 14:57
- Added camelCase to snake_case conversion.
- Improved sortBy parameter handling.
- Enhanced query parameter parsing.
When creating new headlines or sources, the API was failing with a
database error because it tried to insert nested `source`, `category`,
and `headquarters` objects directly into the database. The database
schema expects flattened foreign key columns (`source_id`, `category_id`,
`headquarters_country_id`).

This fix updates the `toJson` functions provided during the setup of
the `headlineRepository` and `sourceRepository` in `AppDependencies`.
These custom functions now correctly flatten the nested objects into
their respective ID fields before the data is sent to the database
client, resolving the "column does not exist" error.
Updated the `headlines` table schema to allow `source_id` and
`category_id` to be nullable, which correctly reflects the optional
`source` and `category` fields in the shared `Headline` model.

Foreign key constraints have been added to these columns to maintain
referential integrity when a value is present. The database seeding
logic has also been updated to handle the potential for null values
in these fields during fixture insertion.
@fulleni fulleni merged commit 5e9f921 into main Jul 7, 2025
1 check failed
@fulleni fulleni deleted the fix_database_bugs branch July 7, 2025 14:49
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @fulleni, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses several database-related issues by updating the database schema, adjusting data serialization logic, and improving API query parameter handling. These changes ensure data integrity and compatibility between the application and the database, while also providing more flexibility in data seeding.

Highlights

  • Database Schema Update: Modified the headlines table schema to allow source_id and category_id to be nullable, establishing foreign key relationships with the sources and categories tables.
  • Data Serialization: Adjusted the serialization logic for Headline and Source objects to handle nested objects correctly when interacting with the database. This involves extracting IDs from nested source, category, and headquarters objects and removing the original nested objects before saving to the database.
  • API Query Parameter Handling: Implemented a function to convert camelCase query parameters (e.g., createdAt) to snake_case (e.g., created_at) for database queries, ensuring compatibility between client requests and database schema.
  • Data Seeding: Updated the database seeding service to accommodate nullable source_id and category_id fields in the headlines table, removing the previous requirement for these fields to be non-null during seeding.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes database bugs by updating the database schema to allow nullable foreign keys and converting API query parameters from camelCase to snake_case. There is some code duplication introduced in the repository toJson converters and the DatabaseSeedingService that should be addressed.

Comment on lines +109 to +110
source_id TEXT REFERENCES sources(id),
category_id TEXT REFERENCES categories(id),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Consider adding NOT NULL constraints to the source_id and category_id columns to enforce data integrity, since these columns are references to other tables.

            source_id TEXT REFERENCES sources(id) NOT NULL,
            category_id TEXT REFERENCES categories(id) NOT NULL,

Comment on lines +118 to +132
(headline) {
final json = headline.toJson();
// The database expects source_id and category_id, not nested objects.
// We extract the IDs and remove the original objects to match the
// schema.
if (headline.source != null) {
json['source_id'] = headline.source!.id;
}
if (headline.category != null) {
json['category_id'] = headline.category!.id;
}
json.remove('source');
json.remove('category');
return json;
},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This change introduces a duplication of logic. The logic to prepare Headline objects for the database now exists here in the repository configuration and also in lib/src/services/database_seeding_service.dart (lines 265-266).

This duplication can lead to maintenance issues, where a change in the database schema would require updates in multiple places. To improve maintainability, consider centralizing this database-specific serialization logic, such as by adding a toDbJson() method to the Headline data model.

Comment on lines +164 to +172
(source) {
final json = source.toJson();
// The database expects headquarters_country_id, not a nested object.
// We extract the ID and remove the original object to match the
// schema.
json['headquarters_country_id'] = source.headquarters?.id;
json.remove('headquarters');
return json;
},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This change introduces a duplication of logic. The logic to prepare Source objects for the database now exists here in the repository configuration and also in lib/src/services/database_seeding_service.dart (lines 232-233).

To improve maintainability, consider centralizing this database-specific serialization logic, such as by adding a toDbJson() method to the Source data model.

@@ -109,10 +119,15 @@ Future<Response> _handleGet(
final queryParams = context.request.uri.queryParameters;
final startAfterId = queryParams['startAfterId'];
final limitParam = queryParams['limit'];
final sortBy = queryParams['sortBy'];
final sortByParam = queryParams['sortBy'];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider renaming sortByParam to sortBy to maintain consistency and avoid confusion, since it is immediately assigned to sortBy on line 129.

  final sortBy = queryParams['sortBy'];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant